package com.inwavetech.os.util.request;

import com.inwavetech.os.util.WsseRequestConfigurator;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;

import java.io.File;
import java.io.FileInputStream;

/**
 * Created by jgskin on 2/24/14.
 */
public class MultipartPost extends HttpRequest {

    private MultipartEntityBuilder builder;

    public MultipartPost() {
        super();
    }

    public MultipartPost(AuthRequestConfigurator wsseRequestConfigurator) {
        super(wsseRequestConfigurator);
    }

    @Override
    protected HttpUriRequest createMethod(String url) {
        HttpPost method = new HttpPost(url);
        method.setEntity(getBuilder().build());

        return method;
    }

    public MultipartPost addParam(String name, String value) {
        getBuilder().addTextBody(name, value);

        return this;
    }

    public MultipartPost addFileInputStream(String name, File file) {
        getBuilder().addPart(name, new FileBody(file));

        return this;
    }

    public MultipartEntityBuilder getBuilder() {
        if (builder == null) {
            builder = MultipartEntityBuilder.create();
        }

        return builder;
    }

}
